package org.star.dolphin.service;

import org.star.dolphin.domain.Email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.activation.DataSource;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

@Service
public class EmailService {

    private static final Logger log = LoggerFactory.getLogger(EmailService.class);

    @Value("${spring.mail.username:}")
    private String emailForm = "";

    @Autowired(required = false)
    private JavaMailSender javaMailSender;

    /**
     * 发送简单的邮件
     *
     * @param email
     */
    @Async
    public void sendSimpleMail(Email email) {
        try {
            //log.info("准备发送一封简单的邮件~");
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(emailForm);
            //log.info(Arrays.toString(email.getTo()));
            message.setTo(email.getTo());
            message.setSubject(email.getSubject());
            message.setText(email.getText());
            if (email.getCc() != null) {
                message.setCc(email.getCc());
            }
            javaMailSender.send(message);
            //log.info("简单的邮件发送完成~");
        } catch (Exception e) {
            log.error("邮件发送错误", e);
        }
    }

    /**
     * 发送HTML格式邮件
     *
     * @param email
     */
    @Async
    public void sendHtmlMail(Email email) {
        MimeMessage message = null;
        try {
            message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailForm);
            helper.setTo(email.getTo());
            helper.setSubject(email.getSubject());
            // 发送htmltext值需要给个true，不然不生效
            helper.setText(email.getText(), true);
            if (email.getCc() != null) {
                helper.setCc(email.getCc());
            }
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("邮件发送错误", e);
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param email
     */
    @Async
    public void sendAttachmentsMail(Email email) {
        MimeMessage message = null;
        try {
            message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailForm);
            helper.setTo(email.getTo());
            helper.setSubject(email.getSubject());
            helper.setText(email.getText());
            if (email.getCc() != null) {
                helper.setCc(email.getCc());
            }
            // 用流的形式发送附件邮件
            DataSource source = new ByteArrayDataSource(email.getIs(), "application/msexcel");
            helper.addAttachment(email.getFileName(), source);
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("邮件发送错误", e);
        }
    }

}
